import { Container } from 'inversify';
import { ISessionService } from '../../application/interfaces/ISessionService';
import { IMessageService } from '../../application/interfaces/IMessageService';
import { IAgentService } from '../../application/interfaces/IAgentService';
import { SessionService } from '../../application/services/session.service';
import { MessageService } from '../../application/services/message.service';
import { AgentService } from '../../application/services/agent.service';
import { IAuthService } from '../../application/interfaces/IAuthService';
import { AuthService } from '../../application/services/auth.service';
import { IUserService } from '../../application/interfaces/IUserService';
import { UserService } from '../../application/services/user.service';
import { IRepository, IUserRepository, IMessageRepository } from '../database/repositories/interfaces/IRepository';
import { SessionRepository } from '../database/repositories/session.repository';
import { MessageRepository } from '../database/repositories/message.repository';
import { UserRepository } from '../database/repositories/user.repository';
import { TokenRepository } from '../database/repositories/token.repository';
import { ConfigService } from '../config/config.service';
import { LoggerService } from '../logging/logger.service';
import { IAgent } from '../../domain/agents/interfaces/IAgent';
import { DefaultAgent } from '../../domain/agents/default.agent';
import { DataSource } from 'typeorm';
import { createDataSource } from '../database/typeorm.config';
import { WsServer } from '../../transport/websocket/ws.server';
import { WsHandler } from '../../transport/websocket/ws.handler';
import { TYPES } from './types';
import { User } from '../../domain/entities/user.entity';
import { Token } from '../../domain/entities/token.entity';
import { Session } from '../../domain/entities/session.entity';
import { Message } from '../../domain/entities/message.entity';

export const container = new Container();

// 服务层
container.bind<ISessionService>(TYPES.SessionService).to(SessionService);
container.bind<IMessageService>(TYPES.MessageService).to(MessageService);
container.bind<IAgentService>(TYPES.AgentService).to(AgentService);
container.bind<IAuthService>(TYPES.AuthService).to(AuthService);
container.bind<IUserService>(TYPES.UserService).to(UserService);

// 仓储层
container.bind<IRepository<Session>>(TYPES.SessionRepository).to(SessionRepository);
container.bind<IMessageRepository<Message>>(TYPES.MessageRepository).to(MessageRepository);
container.bind<IUserRepository<User>>(TYPES.UserRepository).to(UserRepository);
container.bind<IRepository<Token>>(TYPES.TokenRepository).to(TokenRepository);

// 基础设施
container.bind<ConfigService>(TYPES.ConfigService).to(ConfigService).inSingletonScope();
container.bind<LoggerService>(TYPES.LoggerService).to(LoggerService).inSingletonScope();
container.bind<IAgent>(TYPES.Agent).to(DefaultAgent);
container.bind<DataSource>(TYPES.DataSource).toDynamicValue(() => {
  const configService = container.get<ConfigService>(TYPES.ConfigService);
  return createDataSource(configService);
}).inSingletonScope();

// WebSocket相关
container.bind<WsServer>(TYPES.WsServer).to(WsServer).inSingletonScope();
container.bind<WsHandler>(TYPES.WsHandler).to(WsHandler);